package com.lc.guessTheWords.layer;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.lc.guessTheWords.MainActivity;
import com.lc.guessTheWords.R;
import com.lc.guessTheWords.core.GameSystem;
import com.lc.guessTheWords.core.StageManager;
import com.lc.guessTheWords.rank.RankActivity;
import com.lc.guessTheWords.scene.GameScene;
import com.lc.guessTheWords.scene.StageScene;
import com.lc.guessTheWords.scene.ToolShopScene;
import com.lc.guessTheWords.util.ADHelper;
import com.lc.guessTheWords.util.AppControl;
import com.lc.guessTheWords.util.GameUtil;
import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.RotateTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;

/**
 * 主菜单层
 * 
 * @author Administrator
 * 
 */
public class MainLayer extends Layer {

	Sprite background;
	Sprite title_feng;
	Sprite title_kuang;
	Sprite title_tian;
	Sprite title_zi;

	Button btn_newgame;
	Button btn_continue;
	Button btn_toplist;
	Button btn_shop;

	Label studioInfo;

	public MainLayer() {
		// 背景
		background = Sprite.make(GameSystem.tex_main_background);
		background.setAnchor(0, 0);
		background.setPosition(WYPoint.make(0, 0));
		background.setScaleX(GameSystem.Screen_Width
				/ GameSystem.tex_main_background.getWidth());
		background.setScaleY(GameSystem.Screen_Height
				/ GameSystem.tex_main_background.getHeight());
		this.addChild(background);

		// 载入title
		loadTitle();

		loadButtons();

		studioInfo = Label.make("77Studio Presents 2012-2014", 24);
		studioInfo.setColor(WYColor3B.make(200, 200, 200));
		studioInfo.setPosition(240, 5);
		studioInfo.setAnchorY(0);
		addChild(studioInfo);

		// Sprite introNew = Sprite.make(R.drawable.intro_newversion);
		// introNew.setPosition(240, 87);
		// addChild(introNew);

		Button downloadNew = Button.make(R.drawable.intro_newversion,
				R.drawable.intro_newversion, this, "onDownloadNewClicked");
		downloadNew.setClickScale(1.1f);
		downloadNew.setPosition(240, 87);
		this.addChild(downloadNew);

		// 检查更新
		MainActivity.instance.UpdateVersion();
	}

	/**
	 * 下载豪华版
	 */
	public void onDownloadNewClicked() {
		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Uri uri = Uri.parse("http://down.mumayi.com/272352");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				MainActivity.instance.startActivity(intent);
			}
		});
	}

	private void loadButtons() {
		btn_newgame = Button.make(R.drawable.button_startgame_normal,
				R.drawable.button_startgame_press, this, "onNewGameClicked");
		btn_newgame.setPosition(240, 531);
		this.addChild(btn_newgame);

		btn_continue = Button.make(R.drawable.button_continue_normal,
				R.drawable.button_continue_press, this, "onContinueClicked");
		btn_continue.setPosition(240, 438);
		this.addChild(btn_continue);

		btn_toplist = Button.make(R.drawable.button_toplist_normal,
				R.drawable.button_toplist_press, this, "onToplistClicked");
		btn_toplist.setPosition(240, 345);
		this.addChild(btn_toplist);

		btn_shop = Button.make(R.drawable.button_shop_normal,
				R.drawable.button_shop_press, this, "onShopClicked");
		btn_shop.setPosition(240, 252);
		if (!AppControl.isSwitcherOn() || StageManager.instance.isShowedShop()) {
			this.addChild(btn_shop);
			if (!StageManager.instance.isShowedShop()) {
				StageManager.instance.setShowedShop(true);
				MainActivity.instance.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						AlertDialog.Builder builder = new Builder(
								MainActivity.instance);
						builder.setTitle("提示");
						builder.setMessage("去看看道具商店吧，您可以免费获取金币换取提示以及解锁关卡哦！");
						builder.setPositiveButton("OK",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.dismiss();
									}
								});
						builder.create().show();
					}
				});
			}
		} else {
			btn_newgame.setPosition(240, 491);
			btn_continue.setPosition(240, 398);
			btn_toplist.setPosition(240, 305);
		}
	}

	private void loadTitle() {
		title_feng = Sprite.make(GameSystem.tex_title_feng);
		title_feng.setPosition(73, 711);

		title_kuang = Sprite.make(GameSystem.tex_title_kuang);
		title_kuang.setPosition(186, 711);

		title_tian = Sprite.make(GameSystem.tex_title_tian);
		title_tian.setPosition(297, 711);

		title_zi = Sprite.make(GameSystem.tex_title_zi);
		title_zi.setPosition(408, 711);

		addChild(title_feng);
		addChild(title_kuang);
		addChild(title_tian);
		addChild(title_zi);

		// 动画
		setRotate(title_feng, 0.9f, 3);
		setRotate(title_kuang, 0.8f, 3);
		setRotate(title_tian, 1.1f, 3);
		setRotate(title_zi, 0.9f, 3);

	}

	private void setRotate(Sprite target, float time, float toAngle) {
		IntervalAction rotate = (IntervalAction) RotateTo.make(time,
				toAngle * -1, toAngle).autoRelease();
		IntervalAction back = (IntervalAction) rotate.reverse().autoRelease();
		IntervalAction seq = (IntervalAction) Sequence.make(rotate, back)
				.autoRelease();
		Action repeat = (Action) RepeatForever.make(seq).autoRelease();
		target.runAction(repeat);
	}

	public void onNewGameClicked() {
		Log.d("click", "new game");
		GameUtil.switchSceneBottomSlide(StageScene.make());
		StageScene.make().refreshStages();
		// ADHelper.hideAd();
	}

	public void onContinueClicked() {
		Log.d("click", "continue");
		if (StageManager.instance.getLastStage() > 0) {
			GameUtil.switchSceneFlipX(GameScene.make());
			GameLayer.instance.startGame(StageManager.instance.getLastStage());
			// 显示广告
			MainActivity.instance.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					ADHelper.showAd();
				}
			});
		} else {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(MainActivity.instance, "没有保存的游戏记录",
							Toast.LENGTH_LONG).show();
				}
			});

		}
	}

	public void onToplistClicked() {
		Log.d("click", "toplist");
		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// Toast.makeText(MainActivity.instance,
				// "暂未开放该功能，请期待新版本，谢谢您的支持",
				// Toast.LENGTH_LONG).show();
				// ADHelper.hideAd();
				Intent intent = new Intent(MainActivity.instance,
						RankActivity.class);
				MainActivity.instance.startActivity(intent);
			}
		});
	}

	public void onShopClicked() {
		Log.d("click", "Shop");
		GameUtil.switchSceneBottomSlide(ToolShopScene.make());
		ToolShopScene.make().updateCoins();
	}

}
